

var prefix = "http://116.62.135.247:80";
// var prefix = "http://127.0.0.1:3000";


function login(account,password,callback){
    $.ajax({
        type: "POST",
        url: prefix+"/api/user/login/",
        async: false,
        data:{
            username: account,
            password: password
        },
        success: function(res) {
            console.log("登录回调")
            console.log("登录取出数据")
            if(callback){
                callback(res)
            }
        },
        fail:function (res) {
            if(callback){
                callback(res)
            }
        }
    });
}

function register(account,password,callback) {


    $.ajax({
        type: "POST",
        url: prefix+"/api/user/register/",
        async: false,
        data:{
            username: account,
            password: password
        },
        success: function(res) {
            console.log("登录回调")
            console.log("登录取出数据")
            if(callback){
                callback(res)
            }
        },
        fail:function (res) {
            if(callback){
                callback(res)
            }
            console.log()
            alert("失败");
        }
    });
}

function getUserInfo(callback) {

    const token = $.cookie("userToken");
    console.log(token)
    $.ajax({
        type: "POST",
        url: prefix+"/api/user/userInfo/",
        async: false,
        data:{},
        headers: { authorization: token },
        success: function(res) {
            console.log("获取用户数据回调")
            if(callback){
                callback(res)
            }
        },
        fail:function (res) {
            if(callback){
                console.log(res.error)
                callback(res)
            }
        }
    });
}

function postAddress(token,loc,trace,callback) {
    console.log('发起请求');
    var isMine = 0;
    if(!trace){
        isMine = 1;
    }
    $.ajax({
        type: "POST",
        url: prefix+"/api/user/detective/",
        async: false,
        headers: {},
        data:{
            nation:loc.nation,
            province:loc.province,
            district:loc.district,
            city:loc.city,
            addr:loc.addr,
            adcode:loc.adcode,
            trace:trace,
            token:token,
            isMine:isMine,
            type:loc.type
        },
        success: function(res) {
            console.log("上传地址回调")
            if(callback){
                callback(res)
            }
        },
        fail:function (res) {
            if(callback){
                callback(res)
            }

        }
    });
}


function getAddressList(callback) {
    console.log('获取地址');
    const token = $.cookie("userToken");
    $.ajax({
        type: "GET",
        url: prefix+"/api/user/detectiveaddresslist/",
        async: false,
        headers: {authorization: token},
        data:{

        },
        success: function(res) {
            console.log("上传地址回调")
            if(callback){
                callback(res)
            }
        },
        fail:function (res) {
            if(callback){
                callback(res)
            }
            console.log(res)
        }
    });
}



